package com.zara.store.client.clientmodel;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.List;

import com.zara.store.common.InvoiceItemVO;
import com.zara.store.common.InvoiceVO;
import com.zara.store.common.PalcItemVO;
import com.zara.store.common.PalcItemViewVO;
import com.zara.store.common.PalcVO;
import com.zara.store.common.ProductVO;
import com.zara.store.common.StoreProxy;

public class RetailBasket implements Basket {

	private static final long serialVersionUID = 1L;

	private List<BasketLine> lines = new ArrayList<BasketLine>();
	
	private int rowIndex;

	private BasketLine line;

	public BigDecimal getTotal() {
		BigDecimal total = BigDecimal.ZERO;
		for(BasketLine l: lines) {
			total = total.add(l.getLineTotal());
		}
		return total;
	}

	public void removeLine(int i) {
		setRowIndex(i);
		lines.remove(rowIndex);
	}
	public void setLineAmount(BigDecimal amount) {
		line.setAmount(amount);
	}
	public void setLineOffer(boolean offer) {
		line.setOffer(offer);
	}
	public boolean getLineOffer() {
		return line.getOffer();
	}
	public String getLineProduct() {
		return line.getProductName();
	}
	public BigDecimal getLineAmount() {
		return line.getAmount();
	}
	public BigDecimal getLineTotal() {
		return line.getLineTotal();
	}
	public BigDecimal getLinePrice() {
		return line.getPrice();
	}
	public int size() {
		return lines.size();
	}
	public int getRowIndex() {
		return rowIndex;
	}
	public void setRowIndex(int rowIndex) {
		this.rowIndex = rowIndex;
	}
	public void changeRowIndex(int rowIndex) {
		setRowIndex(rowIndex);
		this.line = lines.get(rowIndex);
	}
	public List<BasketLine> getElements() {
		return Collections.unmodifiableList(new ArrayList<BasketLine>(lines));
	}
	public void addProduct(BasketProduct product) {
		if(product.hasProduct()) {
			ProductVO productVO = product.getProductVO();
//			if(productVO.getStock().equals(BigDecimal.ZERO)) {
//				return;
//			}
			for(int i=0; i<lines.size(); i++) {
				BasketLine l = lines.get(i);
				if(l.getProductId()==productVO.getId()) {
					//si tengo el producto
					changeRowIndex(i);
					BigDecimal newAmount = getLineAmount().add(BigDecimal.ONE);
//					if(newAmount.compareTo(productVO.getStock()) > 0) {
//						return;
//					}
					setLineAmount(newAmount);
					return;
				}
			}
			setRowIndex(lines.size());
			line = new RetailBasketLine(product);
			lines.add(line);
		}
	}
	public boolean hasOfferPrice() {
		return line.hasOfferPrice();
	}
	public boolean hasLine() {
		return line != null;
	}

	public void removeProduct(int i) {
		this.removeLine(i);
	}

	public void changeProductOfferAndAmount(int i, boolean offer, String amount) {
		changeRowIndex(i);
		setLineOffer(offer);
//		if(newAmount.compareTo(productVO.getStock()) > 0) {
//			return;
//		}
		changeLineAmount(amount);
	}

	private void changeLineAmount(String amount) {
		try {
			Double.valueOf(amount);
			setLineAmount(new BigDecimal(amount));
		} catch(NumberFormatException e) { //validacion
		}
	}
	
	public void changeProductAmount(int i, String amount) {
		//verificar i
		changeRowIndex(i);
		changeLineAmount(amount);
	}

	public void addPalcItemList() {
		Collection<PalcItemViewVO> palcItemList = StoreProxy.getInstance().queryPalcProductList();
		for(PalcItemViewVO item: palcItemList) {
			BasketProduct product = new RetailBasketProduct(item);
			this.addProduct(product);
		}
	}

	public InvoiceVO createInvoice(InvoiceVO invoiceVO) {
		invoiceVO.setTotal(getTotal());
		invoiceVO.setSubTotal(getTotal());
		for(BasketLine element: lines) {
			invoiceVO.addLine(new InvoiceItemVO(element.getProductId(), element.getAmount(), element.getPrice(), element.getLineTotal()));
		}
		return StoreProxy.getInstance().createInvoice(invoiceVO);
	}

	public PalcVO createPalc() {
		List<PalcItemVO> palcOrder = new ArrayList<PalcItemVO>();
		for(BasketLine element: lines) {
			palcOrder.add(new PalcItemVO(element.getProductId(), element.getAmount()));
		}
		return StoreProxy.getInstance().createPalc(palcOrder);
	}
	
}
